﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class ShoppingCartItemTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase(1)]
        public void Can_CRUD(decimal quantity)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;
            var customer = Processor.Execute(new CreateCustomer
            {
                AccountID = account.EntityID
            }).Result;
            var salesItem = Processor.Execute(new CreateSalesItem
            {
                SellerID = seller.EntityID,
                CatalogItemID = Processor.Execute(new CreateCatalogItem
                {
                    SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID
                }).Result.EntityID,
                SKU = "xy",
                Price = 1
            }).Result;
            var cart = Processor.Execute(new CreateShoppingCart
            {
                CustomerID = customer.EntityID
            }).Result;

            #endregion

            var item = Processor.Execute(new CreateShoppingCartItem
            {
                CartID = cart.EntityID,
                SalesItemID = salesItem.EntityID,
                Price = salesItem.Price,
                Quantity = quantity
            }).Result;

            Assert.NotNull(item);
            item.Quantity.AreEqual(quantity);
            item.AreEqual(Processor.Execute(new GetShoppingCartItem
            {
                EntityID = item.EntityID
            }).Result);

            var upd_item = Processor.Execute(new UpdateShoppingCartItem
            {
                EntityID = item.EntityID,
                Price = item.Price,
                Quantity = quantity
            }).Result;

            Assert.NotNull(upd_item);
            upd_item.AreEqual(item);
            upd_item.Quantity.AreEqual(quantity);

            Processor.Execute(new DeleteShoppingCartItem
            {
                EntityID = item.EntityID
            });
        }

        [TestCase(1,0)]
        [TestCase(1, -1)]
        [TestCase(0, 1)]
        [TestCase(-1, 1)]
        public void Cannot_CreateUpdate_If_QtyOrPrice_LtOrEq_Zero(decimal price, decimal qty)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateShoppingCartItem
            {
                Quantity = qty,
                Price = price
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateShoppingCartItem
            {
                Quantity = qty,
                Price = price
            }));
        }
    }
}
